/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.util.Stack;

/**
 * Created by hk on 2018/4/23.
 */
public class ActivityManagerUtils {

    private static Stack<Activity> sActivityStack;
    private static volatile ActivityManagerUtils sSingleton;

    private ActivityManagerUtils() {
    }

    /**
     * 获取单例
     *
     * @return 实例
     */
    public static ActivityManagerUtils getInstance() {
        if (sSingleton == null) {
            synchronized (ActivityManagerUtils.class) {
                if (sSingleton == null) {
                    sSingleton = new ActivityManagerUtils();
                }
            }
        }
        return sSingleton;
    }

    /**
     * 添加Activity到堆栈
     *
     * @param activity Activity
     */
    public void addActivity(Activity activity) {
        if (sActivityStack == null) {
            sActivityStack = new Stack<>();
        }
        sActivityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     *
     * @return current Activity
     */
    public Activity currentActivity() {
        Activity activity = sActivityStack.lastElement();
        return activity;
    }

    public String getCurrentName() {
        return currentActivity().getComponentName().getClassName();
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = sActivityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     *
     * @param activity Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            sActivityStack.remove(activity);
            activity.finish();
        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls class
     */
    public void finishActivity(Class<?> cls) {
        Stack<Activity> activitys = new Stack<>();
        for (Activity activity : sActivityStack) {
            if (activity.getClass().equals(cls)) {
                activitys.add(activity);
            }
        }

        for (Activity activity : activitys) {
            finishActivity(activity);
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = sActivityStack.size(); i < size; i++) {
            if (null != sActivityStack.get(i)) {
                sActivityStack.get(i).finish();
            }
        }
        sActivityStack.clear();
    }

    /**
     * 应用退出，结束所有的activity
     *
     * @param context Context
     */

    public void exit(Context context) {
        finishAllActivity();
        //        System.exit(0);
        //创建ACTION_MAIN
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //启动ACTION_MAIN
        context.startActivity(intent);
        //android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * activity是否存在
     *
     * @param cls class
     * @return boolean
     */
    public boolean getActivity(Class<?> cls) {
        if (sActivityStack == null || sActivityStack.size() <= 0) {
            return false;
        }
        for (Activity activity : sActivityStack) {
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }
        return false;
    }
}
